package schoolmath.web.services.valueEncoders;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.tapestry5.ValueEncoder;

public class Encoder<T> implements ValueEncoder<T> {

	private Class<T> cls;
	private Gson gson;

	public Encoder(Class<T> cls) {
		this.cls = cls;

		gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
	}

	@Override
	public String toClient(T value) {
		return gson.toJson(value);
	}

	@Override
	public T toValue(String clientValue) {
		return gson.fromJson(clientValue, cls);
	}

}
